var gulp = require('gulp');
var del = require('del');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var minifyCss = require('gulp-minify-css');
var gutil = require('gulp-util');

// 清理 dist 目录
gulp.task('clean', function(cb) {
    del([
        'dist/*',
        'muse-autocomplete.html',
        'muse-popup.html'
      ], cb);
});

// 将所有css文件连接为一个文件并压缩，存到 dist/css
gulp.task('concatCss', function() {
    gulp.src(['muse-autocomplete/*.css', 'muse-pop/*.css'])
        .pipe(concat('muses.min.css'))
        .pipe(minifyCss())
        .pipe(gulp.dest('dist'));
});

// 将所有js文件连接为一个文件并压缩，存到 dist/js
gulp.task('concatJs', function() {
    gulp.src(['muse-autocomplete/*.js', 'muse-pop/*.js'])
        .pipe(concat('muses.min.js'))
        .pipe(uglify().on('error', function (err) {
            gutil.log(err);
            this.emit('end');
        }))
        .pipe(gulp.dest('dist'));
});

// 复制文件
gulp.task('copy', function() {
    gulp.src('img/*')
        .pipe(gulp.dest('dist/img'));

    gulp.src('demo/muse-autocomplete.html')
        .pipe(gulp.dest('./'));

    gulp.src('demo/muse-popup.html')
        .pipe(gulp.dest('./'));
});

gulp.task('default', ['clean', 'concatCss', 'concatJs', 'copy']);
